package com.pyb.java.commonutil.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.pyb.java.commonutil.R;
import com.pyb.java.commonutil.utils.AppUtils;

import java.io.File;

public class GlideImageLoader implements ImageDisplayLoader {
    @Override
    public void display(final ImageView imageView, final String url, final ImageLoadListener listener, DisplayOption opts) {

        Glide.with(AppUtils.getInstance().getApp())
                .asBitmap()
                .load(url)
                .addListener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        listener.onLoadFail(url, imageView, e);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        listener.onLoadSuccess(imageView, resource);
                        return false;
                    }
                }).diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .into(imageView);
    }

    @Override
    public void display(ImageView imageView, String url) {
        Glide.with(imageView.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .dontAnimate()
                .into(imageView);
    }

    @Override
    public void display(ImageView imageView, String url, int width, int height) {
        Glide.with(imageView.getContext())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .override(width, height)
                .into(imageView);
    }

    @Override
    public void display(final ImageView imageView, String url, int defaultImag) {
        Glide.with(imageView.getContext())
                .load(url)
                .dontAnimate()
                .placeholder(defaultImag)
                .error(defaultImag)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .into(imageView);
    }

    @Override
    public void display(ImageView imageView, Uri url, int defaultImage) {
        Glide.with(imageView.getContext())
                .load(url)
                .dontAnimate()
                .placeholder(defaultImage)
                .error(defaultImage)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .into(imageView);
    }

    @Override
    public void display(final ImageView imageView, String url, Drawable drawable) {
        Glide.with(imageView.getContext()).load(url)
                .dontAnimate()
                .placeholder(drawable)
                .error(drawable)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(imageView);
    }

    @Override
    public void displayFile(ImageView imageView, File file, int defaultImage) {
        Glide.with(imageView.getContext()).load(file)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .error(defaultImage)
                .placeholder(defaultImage)
                .into(imageView);
    }

    @Override
    public void displayWithCorner(ImageView image, String url,int defaultImg) {
        Glide.with(image.getContext())
                .load(url)
                .centerCrop()
                .placeholder(defaultImg)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(image);
    }

    @Override
    public void displayWithCircle(ImageView image, String url) {
        Glide.with(image.getContext()).
                load(url)
                .apply(RequestOptions.bitmapTransform(new CircleCrop())).into(image);
    }

    @Override
    public void displayWithCircle(ImageView image, int imageRes) {
        Glide.with(image.getContext()).load(imageRes)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(image);
    }

    @Override
    public void displayWithByte(ImageView image, byte[] bytes, int defaultImage) {
        Glide.with(image.getContext())
                .load(bytes).placeholder(defaultImage).into(image);
    }

    @Override
    public void resumeRequest(Context context) {
        Glide.with(context).resumeRequests();
    }

    @Override
    public void pauseRequest(Context context) {
        Glide.with(context).pauseRequests();
    }


    @Override
    public Bitmap syncLoad(File file) {
        return null;
    }

    @Override
    public Bitmap syncLoad(String url) {
        return null;
    }

    @Override
    public void syncLoad(String url, ImageLoadListener listener) {

    }
}
